package com.jswj.word.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.jswj.media.service.IMediaService;
import com.jswj.word.domain.WordExt;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jswj.word.mapper.WordMapper;
import com.jswj.word.domain.Word;
import com.jswj.word.service.IWordService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 关键词Service业务层处理
 *
 * @author whh
 * @date 2021-06-26
 */
@Service
public class WordServiceImpl implements IWordService {
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private IMediaService mediaService;

    /**
     * 查询关键词
     *
     * @param id 关键词ID
     * @return 关键词
     */
    @Override
    public Word selectWordById(Long id) {
        return wordMapper.selectWordById(id);
    }

    @Override
    public List<Word> selectAllWord() {
        return wordMapper.selectAllWord();
    }

    /**
     * 查询关键词列表
     *
     * @param word 关键词
     * @return 关键词
     */
    @Override
    public List<WordExt> selectWordList(Word word) {
        List<Word> words = wordMapper.selectWordList(word);
        if(CollectionUtils.isEmpty(words)) {
            return Collections.EMPTY_LIST;
        }
        List<WordExt> list = new ArrayList<>(words.size());
        words.forEach(w -> {
            WordExt wordExt = new WordExt();
            wordExt.setId(w.getId());
            wordExt.setWordGroup(w.getWordGroup());
            wordExt.setWords(w.getWords());
            wordExt.setMediaIds(w.getMediaIds());
            wordExt.setMediaList(mediaService.selectMediasByIds(w.getMediaIds()));
            list.add(wordExt);
        });
        return list;
    }

    /**
     * 新增关键词
     *
     * @param word 关键词
     * @return 结果
     */
    @Override
    public int insertWord(Word word) {
        return wordMapper.insertWord(word);
    }

    /**
     * 修改关键词
     *
     * @param word 关键词
     * @return 结果
     */
    @Override
    public int updateWord(Word word) {
        return wordMapper.updateWord(word);
    }

    /**
     * 删除关键词对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWordByIds(String ids) {
        return wordMapper.deleteWordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除关键词信息
     *
     * @param id 关键词ID
     * @return 结果
     */
    @Override
    public int deleteWordById(Long id) {
        return wordMapper.deleteWordById(id);
    }
}
